package com.jsbb.service.sys;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jsbb.dto.sys.PersonalDataUpdateDto;
import com.jsbb.dto.sys.PwdUpdateDto;
import com.jsbb.dto.sys.*;
import com.jsbb.exception.ServiceException;
import com.jsbb.mapper.sys.SysRoleMapper;
import com.jsbb.mapper.sys.SysUserMapper;
import com.jsbb.mapper.sys.SysUserRoleMapper;
import com.jsbb.model.sys.SysUser;
import com.jsbb.model.sys.SysUserRole;
import com.jsbb.util.IdWorkerUtils;
import com.jsbb.vo.PageVo;
import com.jsbb.vo.sys.SysUserDetailVo;
import com.jsbb.vo.sys.SysUserListVo;
import lombok.extern.slf4j.Slf4j;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户Service
 *
 * @author w
 * @date 2021-03-05
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    public SysUser selectByLoginName(String loginName) {
        return sysUserMapper.selectByLoginName(loginName);
    }

    public SysUserDetailVo get(Long id) {
        //获取用户
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(id);
        if (sysUser == null) {
            throw new ServiceException("数据不存在");
        }
        SysUserDetailVo userVo = new SysUserDetailVo();
        BeanUtils.copyProperties(sysUser, userVo);

        //获取用户角色
        List<SysRoleDto> roleList = sysRoleMapper.selectByUserId(id);
        List<Long> roleIdList = roleList.stream().map(SysRoleDto::getId).collect(Collectors.toList());
        userVo.setRoleId(roleIdList);
        return userVo;
    }

    public PageVo<SysUserListVo> list(SysUserQueryDto queryDto) {
        Page page = PageHelper.startPage(queryDto.getPageNo(), queryDto.getPageSize());
        List<SysUserDetailDto> list = sysUserMapper.selectByCondition(queryDto);
        List<SysUserListVo> userVoList = new ArrayList<>();
        for (SysUserDetailDto sysUser : list) {
            SysUserListVo userVo = new SysUserListVo();
            BeanUtils.copyProperties(sysUser, userVo);
            userVoList.add(userVo);
            if (sysUser.getRoleList() != null) {
                List<String> roleNameList = sysUser.getRoleList().stream().map(SysRoleDto::getRoleName).collect(Collectors.toList());
                userVo.setRoleName(roleNameList);
            }
        }
        return new PageVo((int) page.getTotal(), userVoList);
    }

    public void save(SysUserSaveDto saveDto) {
        //新增用户
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(saveDto, sysUser);
        sysUser.setId(IdWorkerUtils.idWorker.nextId());
        sysUser.setPwd(BCrypt.hashpw("123456", BCrypt.gensalt()));
        sysUser.setType(1);
        sysUser.setState(1);
        sysUser.setCreateTime(new Date());
        sysUser.setUpdateTime(new Date());
        sysUserMapper.insert(sysUser);

        //新增用户关联角色
        if (saveDto.getRoleId() != null) {
            for (Long roleId : saveDto.getRoleId()) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setId(IdWorkerUtils.idWorker.nextId());
                sysUserRole.setUserId(sysUser.getId());
                sysUserRole.setRoleId(roleId);
                sysUserRoleMapper.insert(sysUserRole);
            }
        }
    }

    public void delete(Long id) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(id);
        if (sysUser == null) {
            throw new ServiceException("数据不存在");
        }
        sysUser.setState(2);
        sysUserMapper.updateByPrimaryKey(sysUser);
    }

    public void update(SysUserUpdateDto updateDto) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(updateDto.getId());
        if (sysUser == null) {
            throw new ServiceException("数据不存在");
        }
        //修改用户
        BeanUtils.copyProperties(updateDto, sysUser);
        sysUserMapper.updateByPrimaryKey(sysUser);

        //删除用户关联角色
        sysUserRoleMapper.deleteByUserId(sysUser.getId());

        //新增用户关联角色
        if (updateDto.getRoleId() != null) {
            for (Long roleId : updateDto.getRoleId()) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setId(IdWorkerUtils.idWorker.nextId());
                sysUserRole.setUserId(sysUser.getId());
                sysUserRole.setRoleId(roleId);
                sysUserRoleMapper.insert(sysUserRole);
            }
        }
    }

    public void baseDataUpdate(PersonalDataUpdateDto updateDto) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(updateDto.getId());
        if (sysUser == null) {
            throw new ServiceException("数据不存在");
        }
        BeanUtils.copyProperties(updateDto, sysUser);
        sysUserMapper.updateByPrimaryKey(sysUser);
    }

    public void pwdUpdate(PwdUpdateDto updateDto) {
    }
}
